﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nova_Common;
using Nova_MOD;
using Nova_BLL;
using Nova_DAL;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web.Profile;
using System.Drawing;
public partial class Report_StockSearch : System.Web.UI.Page
{
    DbHelper access = new DbHelper();//本地的正常数据库
    //DbHelper_JL access = new DbHelper_JL();//使用的WMSJL数据库
    DbCommand cmd = null;
    static DataSet ds = null;
    string strSQL = "";
    private string strBaoJin = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }

    protected void bind()
    {
        this.cmd = this.access.GetSqlStringCommond(this.getSQL());
        ds = this.access.ExecuteDataSet(this.cmd);
        this.gv.DataSource = ds;
        this.gv.DataBind();

    }

    protected string getSQL()
    {
        this.strSQL = @"SELECT Pos_ID,Mat_Num1,Mat_Name,Unit_Base,N01,MiniNum,Mat_Code as Mat_Code,CDT,TS01,S01,TM01,S02,TM03   
                            FROM ( Select MAX(ISNUll(TBP.Pos_ID,'')) Pos_ID,SUM(IsNull(TBS.Mat_Num,0)) Mat_Num1,
                           MAX(ISNUll(TBM.Mat_Name,'')) Mat_Name,
                           MAX(ISNUll(TBM.Unit_Base,'')) Unit_Base,MAX(ISNull(TBM.N01,0)) as N01,MAX(ISNull(TBM.MiniNum,0)) as MiniNum,
                           ISNUll(TBM.Mat_Code,'') Mat_Code,MAX(TBS.CDT) CDT,TBI.TS01,TBM.S01,TBM.TM01,TBM.S02,TBM.TM03
                      From TBase_Material TBM
           LEFT OUTER JOIN TBase_Interface TBI ON TBM.Mat_Code=TBI.Mat_Code
           LEFT OUTER JOIN TBase_Storage TBS ON TBI.Sec_Mat_ID=TBS.Sec_Mat_ID
           LEFT JOIN TRel_TrayPosContainer TRT ON TBS.ContainerID=TRT.ContainerID 
           LEFT JOIN TBase_TrayPos TBT ON TBT.Relations_ID = TRT.Relations_ID 
           LEFT JOIN TRelations TRP ON TRP.Tray_ID = TBT.Tray_ID 
           LEFT JOIN TBase_Position TBP ON TRP.Pos_ID = TBP.Pos_ID 
           WHERE TBM.N01>=0  and  TRP.Pos_ID<>200000000000";

        if (this.txtMat_Code.Text != "")
        {
            this.strSQL = this.strSQL + " AND TBM.Mat_Code like '%" + this.txtMat_Code.Text.ToString().Trim() + "%'";
        }
        if (this.txtMat_Name.Text != "")
        {
            this.strSQL = this.strSQL + " AND TBM.Mat_Name like '%" + this.txtMat_Name.Text.ToString().Trim() + "%'";
        }
        if(!string.IsNullOrEmpty(txt_TS01.Text))
        {
            this.strSQL = this.strSQL + " AND TBI.TS01 like '%" + this.txt_TS01.Text.ToString().Trim() + "%'";

        }
        if ((this.Position1.No_Lane != "0") && (this.Position1.No_Lane != ""))
        {
            this.strSQL = this.strSQL + " AND TBP.No_Lane = '" + this.Position1.No_Lane.ToString() + "'";
        }
        if ((this.Position1.No_Line != "0") && (this.Position1.No_Line != ""))
        {
            this.strSQL = this.strSQL + " AND TBP.No_Line = '" + this.Position1.No_Line.ToString() + "'";
        }
        if ((this.Position1.No_Col != "0") && (this.Position1.No_Col != ""))
        {
            this.strSQL = this.strSQL + " AND TBP.No_Col = '" + this.Position1.No_Col.ToString() + "'";
        }
        if ((this.Position1.No_Layer != "0") && (this.Position1.No_Layer != ""))
        {
            this.strSQL = this.strSQL + " AND TBP.No_Layer = '" + this.Position1.No_Layer.ToString() + "'";
        }
        if (this.txtTray_ID.Text != "")
        {
            this.strSQL = this.strSQL + " AND TRP.Tray_ID like '%" + this.txtTray_ID.Text.ToString() + "%'";
        }
        this.strSQL = this.strSQL + "  GROUP BY TBM.Mat_Code,TBI.TS01,TBM.S01,TBM.TM01,TBM.S02,TBM.TM03 ) AS A ";


        if (DDLMiniNum.SelectedIndex > 0)
        {
            this.strSQL = this.strSQL + "where Mat_Num1<=MiniNum";
        }

        this.strSQL = this.strSQL + "   Order By A.CDT DESC ";

        return this.strSQL;
    }

    /// <summary>
    /// 少于存库少于安全库存量，变红色！
    /// 安全库存量在物料基本表中，此版本没有使用
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    Label label = (Label)e.Row.FindControl("lblN01");
        //    if (label.Text == "")
        //    {
        //        this.strBaoJin = "0";
        //    }
        //    else
        //    {
        //        this.strBaoJin = label.Text.ToString();
        //    }
        //    if (Convert.ToDecimal(e.Row.Cells[3].Text.ToString()) < Convert.ToDecimal(this.strBaoJin))
        //    {
        //        e.Row.ForeColor = Color.FromName("red");
        //    }
        //}

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (DDLMiniNum.SelectedValue == "10")
            {
                e.Row.ForeColor = Color.FromName("red");
            }

            else if (Convert.ToDecimal(e.Row.Cells[3].Text.ToString()) <= Convert.ToDecimal(e.Row.Cells[4].Text.ToString()))
            {
                e.Row.ForeColor = Color.FromName("red");
            }
        }
    }

    protected void ibtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        //base.Response.Clear();
        //base.Response.BufferOutput = true;
        //base.Response.Charset = "utf-8";
        //DateTime now = DateTime.Now;
        //base.Response.AppendHeader("Content-Disposition", "attachment;filename=" + now.ToString("yyyyMMddHHmmss") + ".xls");
        //base.Response.ContentEncoding = Encoding.GetEncoding("utf-8");
        //base.Response.ContentType = "application/ms-excel";
        //this.EnableViewState = false;
        //CultureInfo formatProvider = new CultureInfo("ZH-CN", true);
        //StringWriter writer = new StringWriter(formatProvider);
        //HtmlTextWriter writer2 = new HtmlTextWriter(writer);
        //this.gv.RenderControl(writer2);
        //base.Response.Write(writer.ToString());
        //base.Response.End();
        if (ds == null)
            return;
        DataTable dt = ds.Tables[0];
        Response.Clear();
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Charset = "GB2312";
        Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode("库存总览" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"));
        Response.ContentType = "application/ms-excel";

        Response.Write("物料编码\t物料名称\t物料数量\t生成通知单\t规格\t材质\t长度\t备注\t单位");

        Response.Write(Environment.NewLine);
        foreach (DataRow dr in dt.Rows)
        {

            StringBuilder sb = new StringBuilder();

            sb.Append(dr["Mat_Code"] + "\t" + dr["Mat_Name"] + "\t" + dr["Mat_Num1"] + "\t" + dr["TS01"] + "\t" + dr["S01"]+ "\t" + dr["TM01"]+ "\t"+ dr["TM03"] + "\t" + dr["S02"].ToString().Replace("\r\n","") + "\t" + dr["Unit_Base"]);

            Response.Write(sb.ToString());
            Response.Write(Environment.NewLine);

        }
        Response.End();
    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        this.bind();
    }


    public override void VerifyRenderingInServerForm(Control control)
    {
    }

    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gv.PageIndex = e.NewPageIndex;
        this.bind();
    }

    protected void btnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtMat_Code.Text = "";
        txtMat_Name.Text = "";
        txtTray_ID.Text = "";
        DDLMiniNum.SelectedIndex = 0;
        Position1.No_Lane = "0";
        Position1.No_Layer = "0";
        Position1.No_Line = "0";
        Position1.No_Col = "0";

    }
}